package com.ruoyi.web.controller.wx;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.WxUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 微信用户Controller
 * @author yuanzheng
 * @date 2021-12-25
 */
@RestController
@RequestMapping("/wxUser")
public class WxUserController extends BaseController {
    @Autowired
    private IWxUserService wxUserService;

    @Resource
    private ISysUserService sysUserService;
    /**
     * 查询微信用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WxUser wxUser) {
        startPage();
        List<WxUser> list = wxUserService.selectWxUserList(wxUser);
        return getDataTable(list);
    }

    /**
     * 导出微信用户列表
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:export')")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxUser wxUser) {
        List<WxUser> list = wxUserService.selectWxUserList(wxUser);
        ExcelUtil<WxUser> util = new ExcelUtil<WxUser>(WxUser.class);
        return util.exportExcel(list, "微信用户数据");
    }

    /**
     * 获取微信用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(wxUserService.selectWxUserById(id));
    }

    /**
     * 新增微信用户
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:add')")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUser wxUser) {
        return toAjax(wxUserService.insertWxUser(wxUser));
    }

    /**
     * 修改微信用户
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:edit')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUser wxUser) {
        return toAjax(wxUserService.updateWxUser(wxUser));
    }

    /**
     * 删除微信用户
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:remove')")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(wxUserService.deleteWxUserByIds(ids));
    }

    /**
     * 逻辑删除微信用户
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:remove')")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/logicRemove/{id}")
    public AjaxResult logicRemove(@PathVariable String id) {
        return toAjax(wxUserService.logicDeleteWxUserById(id));
    }

    /**
     * 逻辑批量删除微信用户
     */
    @PreAuthorize("@ss.hasPermi('wx:wxUser:remove')")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/logicBatchRemove/{ids}")
    public AjaxResult logicRemove(@PathVariable String[] ids) {
        return toAjax(wxUserService.logicDeleteWxUserByIds(ids));
    }


    @GetMapping(value = "/updateWxUserInfo")
    public AjaxResult updateWxUserInfo(WxUser wxUser) {
        wxUser = wxUserService.updateWxUserInfo(wxUser);
        AjaxResult ajax = AjaxResult.success(wxUser);
        ajax.put("sysUser", sysUserService.selectUserById(wxUser.getUserId()));
        return ajax;
    }


    @GetMapping(value = "/bindSysUser")
    public AjaxResult bindSysUser(@RequestParam(value="userName") String userName, @RequestParam(value="password") String password) {
        System.out.println(userName);
        System.out.println(password);
        Map<String, Object> map = wxUserService.bindSysUser(userName, password);
        return AjaxResult.success(map);
    }


    @GetMapping(value = "/unBindWxUser")
    public AjaxResult unBindWxUser() {
        return AjaxResult.success(wxUserService.unBindWxUser());
    }


}
